package sample.foo;

import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @date 2020/8/26
 */
@Component
public class SubscribeRunner implements CommandLineRunner {

	private Logger logger = LoggerFactory.getLogger(SubscribeRunner.class);

	@Override
	public void run(String... args) throws Exception {
		NamingService namingService = NamingFactory.createNamingService("localhost:8848");
		namingService.subscribe("foo-app", new EventListener() {
			@Override
			public void onEvent(Event event) {
				NamingEvent namingEvent = (NamingEvent) event;
				for (Instance instance : namingEvent.getInstances()) {
					logger.info("onEvent" + instance.toString());
				}
			}
		});
	}
}
